// pages/create-task/create-task.js
const app = getApp();

Component({

  /**
   * 页面的初始数据
   */
  data: {
    task: {
      id: '',
      content: '',
      datetime: '',
      tag: '',
      belong: '',
      remind: false,
      disable: true,
      x: 0,
      y: 0
    },
    startDate: '',
    date: '',
    time: '',
    priorityIndex: '',
    priorityList: app.globalData.prioritys,
    tagIndex: '',
    tagList: []
  },
  
  observers: {
    'date, time': function (date, time) {
      if (date && time) {
        this.setData({
          'task.datetime': date + ' ' + time
        });
      } else {
        this.setData({
          'task.datetime': ''
        });
      }
    },
  },

  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      this.getStartDate();
      // 初始化本次任务ID
      const id = app.methods.createRandomStr(10);
      this.setData({
        'task.id': id,
        tagList: app.globalData.tags
      });
    },

    getStartDate () {
      const date = new Date();
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();

      this.setData({
        startDate: [year, month, day].join('-')
      });
    },

    bindContentChange (e) {
      const content = e.detail.value;
      this.setData({
        'task.content': content
      });
    },
    bindDateChange (e) {
      const date = e.detail.value;
      this.setData({
        date
      })
    },
    bindTimeChange (e) {
      const time = e.detail.value;
      this.setData({
        time
      })
    },
    bindPriorityChange (e) {
      const index = e.detail.value;
      const belong = this.data.priorityList[index].id;

      this.setData({
        priorityIndex: Number(index),
        'task.belong': belong
      });
    },
    bindTagChange (e) {
      const index = e.detail.value;
      const tag = this.data.tagList[index].label;

      this.setData({
        tagIndex: Number(index),
        'task.tag': tag
      });
    },
    bindRemindChange (e) {
      const value = e.detail.value[0];

      this.setData({
        'task.remind': value
      });
    },

    checkData () {
      const task = this.data.task;
      let res = false;

      if (!task.content) {
        wx.showModal({
          title: '内容未填写完整',
          content: '任务内容是必填项！'
        });
      } else if (!task.belong) {
        wx.showModal({
          title: '内容未填写完整',
          content: '所属象限是必填项！'
        });
      } else {
        res = true;
      }

      return res;
    },
    resetForm () {
      this.setData({
        date: '',
        time: '',
        priorityIndex: '',
        tagIndex: '',
        task: {
          content: '',
          datetime: '',
          tag: '',
          belong: '',
          remind: false,
        }
      });
    },
    submitForm (e) {
      wx.vibrateShort({
        type: 'heavy'
      });

      if (this.checkData()) {
        const dataStr = JSON.stringify(this.data.task);

        // 更新统计数据
        app.globalData.statistics.tasksTotal += 1;

        wx.navigateBack({
          delta: 1
        });;

        // 将本次创建的task存储到storage中
        wx.setStorageSync('create-task', dataStr);
      }
    }
  }
})